<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class App extends MY_Controller {
    
    public function __construct(){
        parent::__construct();
        $this->load->model('app_model');
        $this->load->model('profile_model');
        $this->load->model('index_model');
        $this->load->model('user_model');
    }
    
    public function login(){
        if($_POST){
            $username = $_POST['username'];
            $password = $_POST['password'];
            $user = $this->app_model->applogin($username,$password);
            if(count($user) == 0){
                $data = array('uen'=>$username);
                $company = $this->index_model->getOneByData($data,"company");
                if(count($company) == 0){
                    $user = array('id'=>'0');
                }else{
                    $user = array(
                        'id'=>$company['id'],
                        'username'=>$username,
                        'password'=>$password,
                        'role'=>10
                    );
                }
            }
            echo json_encode($user); 
        }else{
            $array = array('id'=>'0');
            echo json_encode($array);
        }
    }
    
    public function getData(){
        if($_POST){
            $data = $this->app_model->getData();
            echo json_encode($data); 
        }
    }
    
    public function getProfile(){
        if($_POST){
            $data = $this->app_model->getProfile();
            echo json_encode($data); 
        }
    }
    
    public function getProfileDetail(){
        if($_POST){
            $profileid = $_POST['profileid'];
            //根据得到profile信息
            $profile = $this->profile_model->getProfileByID($profileid);
            //是否存在订单
            if($profile['ordersn'] == NULL){//不存在订单
                $order = array(
                    'position'=>'',
                    'gender'=>0,
                    'salary'=>'',
                    'worktype'=>0,
                    'starttime'=>NULL,
                    'endtime'=>NULL,
                    'workintro'=>'',
                    'comment'=>''
                );
                //查询出申请单下的所有工人
                $worker = $this->index_model->getWorkersByProfileID($profile['id']);
                //查询出该申请公司名称
                $company = $this->index_model->getcompanybyid($profile['companyid']);
            }else{//存在订单
                //得到profile名称
                $profilename = $profile['name'];
                $profilenames = explode("_", $profilename);
                //得到订单sn
                $ordersn = $profilenames[0];
                //根据订单sn查询出订单
                $order = $this->profile_model->getOrderBySN($ordersn);
                //得到订单id
                $orderid = $order['id'];
                //查询出订单下的所有工人
                $worker = $this->index_model->getWorkersByOrderID($orderid);
                //查询出该订单公司名称
                $company = $this->index_model->getcompanybyid($order['companyid']);
            }
            //得到公司行业
            $industry = $this->index_model->getByID("industry",$company['industry']);
            if(count($industry) == 0){
                $industry['name'] = "";
            }
            //得到业务员账号id
            $userid = $profile['employeeid'];
            //根据userid得到员工信息
            $employee = $this->user_model->getEmployeeByUserID($userid);
            //根据profileid得到该申请的文档
            $attachments = $this->index_model->getAttachmentsBYAssociatedID($profile['id'],1);
            //构建数据数组
            $data = array(
                'profile'=>$profile,
                'order'=>$order,
                'worker'=>$worker,
                'company'=>$company,
                'employee'=>$employee,
                'industry'=>$industry,
                'attachments'=>$attachments
            );
            echo json_encode($data); 
        }
    }
    
    function getReminder(){
        if($_POST){
            $data = $this->app_model->getReminder();
            echo json_encode($data); 
        }else{
            $sql = "select n.*,w.name,e.name createby from lfsrlzy_notice n "
                . "left join lfsrlzy_workers w on w.id = n.association "
                . "left join lfsrlzy_employee e on n.employeeid = e.userid "
                . "where (n.title like '%%' or w.name like '%%') ";
        

            $employee = $this->db->get_where('employee', array('userid' => 38))->row_array();
            if($employee['department']*1 != 3){
                $sql .= " and n.employeeid = 38 ";
            }else{
                $sql .= " and n.ispublic = 1 ";
            }
            $sql .= " and n.noticedate < ".time()." and n.type = 0";
            $sql .= " order by n.noticedate desc limit 0,10";
            echo $sql;
        }
    }
    
    /**
     * 得到app版本2的首页数据数量
     */
    function getCount(){
        if($_POST){
            //用户id
            $userid = $_POST["userid"];
            $role = $_POST['role'];
//            $userid = $_GET["userid"];
//            $role = $_GET['role'];
            if($role == 10){//公司账号登陆
                $notice = $this->app_model->getNoticeByCompanyId($userid);
                $normal_notice = $this->app_model->getNoticeByCompanyId($userid,0);
                $pickup_notice = $this->app_model->getNoticeByCompanyId($userid,1);
                $process_notice = $this->app_model->getNoticeByCompanyId($userid,6);
                $delivery_notice = $this->app_model->getNoticeByCompanyId($userid,7);
                $course_notice = $this->app_model->getNoticeByCompanyId($userid,8);
                $other_notice = $this->app_model->getNoticeByCompanyId($userid,100);
                $application_candidate = $this->app_model->getApplicationWorkerByCompanyId($userid);
            }else{
                //查询notice的总数
                $notice = $this->index_model->getAllByData(array(),"notice");
                //查询出normal notice的总数
                $normal_notice = $this->index_model->getAllByData(array('type'=>0),"notice");
                //查询出接机notice的总数
                $pickup_notice = $this->index_model->getAllByData(array('type'=>1),"notice");
                $process_notice = $this->index_model->getAllByData(array('type'=>6),"notice");
                $delivery_notice = $this->index_model->getAllByData(array('type'=>7),"notice");
                $course_notice = $this->index_model->getAllByData(array('type'=>8),"notice");
                $other_notice = $this->app_model->getOtherNotice();
                //查询出status为application的工人总数
                $application_candidate = $this->index_model->getAllByData(array('status'=>0),"workers");
            }
            $calendar_num = count($notice);
            $normal_notice_num = count($normal_notice);
            $pickup_notice_num = count($pickup_notice);
            $process_notice_num = count($process_notice);
            $delivery_notice_num = count($delivery_notice);
            $course_notice_num = count($course_notice);
            $other_notice_num = count($other_notice);
            $application_candidate_num = count($application_candidate);
            //返回数据
            $data = array(
                'calendar_num'=>$calendar_num,
                'normal_notice_num'=>$normal_notice_num,
                'pickup_notice_num'=>$pickup_notice_num,
                'process_notice_num'=>$process_notice_num,
                'delivery_notice_num'=>$delivery_notice_num,
                'course_notice_num'=>$course_notice_num,
                'other_notice_num'=>$other_notice_num,
                'application_candidate_num'=>$application_candidate_num
            );
            echo json_encode($data); 
        }
    }
    
    /**
     * 根据post过来的日期得到当月的消息信息
     */
    public function getNotices(){
        if($_POST){
            $current_date = $_POST['date'];
            $year = date("Y",strtotime($current_date));
            $month = date("m",strtotime($current_date));
            $day = $this->getMonthLastDay($month, $year);
            $start_week = date("w",strtotime($year."-".$month.'-01'));
            $days = $this->getDays($day,$start_week,$year,$month);
            echo json_encode($days);
        } 
    }
    
    /**
     * 得到日期及日期包含的notice数据
     * @param type $day
     * @param type $week
     * @param type $year
     * @param type $month
     * @return array
     */
    public function getDays($day,$week,$year,$month){
        $days = array();
        $index = 1;
        for($i = 0;$i < 42;$i++){
            $days[$i] = array();
            if($i < $day+$week && $i >= $week){
                $days[$i]['day'] = $index;
                if($index*1 < 10){
                    $days[$i]['date'] = $year.'-'.$month.'-0'.$index;
                }else{
                    $days[$i]['date'] = $year.'-'.$month.'-'.$index;
                }
                $role = $_POST['role'];
                if($role == 10){
                    $userid = $_POST['userid'];
                    $days[$i]['notices'] = $this->app_model->getNoticeByCompanyId($userid,-1,strtotime($days[$i]['date']));
                }else{
                    $days[$i]['notices'] = $this->index_model->getAllByData(array('noticedate'=>strtotime($days[$i]['date'])),"notice");
                }
                $index++;
            }else{
                $days[$i]['day'] = 0;
                $days[$i]['date'] = 0;
                $days[$i]['notices'] = array();
            }
        }
        return $days;
    }
    
    /**
     * 得到一个月的天数
     * @param type $month 月
     * @param type $year 年
     * @return int 天数
     */
    function getMonthLastDay($month,$year) {
        switch ($month) {
            case 4 :
            case 6 :
            case 9 :
            case 11 :
                $days = 30;
                break;
            case 2 :
                if ($year % 4 == 0) {
                    if ($year % 100 == 0) {
                        $days = $year % 400 == 0 ? 29 : 28;
                    } else {
                        $days = 29;
                    }
                } else {
                    $days = 28;
                }
                break;
            default :
                $days = 31;
                break;
        }
        return $days;
    }
    
    public function getApplicationCandidate(){
        if($_POST){
            $data = $this->app_model->getApplicationCandidate();
            echo json_encode($data); 
        }
    }
}